<template>
  <div class="app-container">
    <el-form :model="form" ref="form" :inline="true">
      <el-form-item label="订单类型" prop="orderType">
        <el-select v-model="form.orderType" filterable clearable>
          <el-option
            v-for="item in dict.type.sys_order_type"
            :key="item.value"
            :label="item.label"
            :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="关键词" prop="keyWord">
        <el-input v-model="form.keyWord" placeholder="请输入"></el-input>
      </el-form-item>
      <el-form-item label="订单号" prop="orderNo">
        <el-input v-model="form.orderNo" placeholder="请输入"></el-input>
      </el-form-item>
      <el-form-item label="客户名称" prop="customerName">
        <el-input v-model="form.customerName" placeholder="请输入"></el-input>
      </el-form-item>
      <el-form-item label="订单状态" prop="statusList">
        <el-select v-model="form.statusList" multiple filterable clearable>
          <el-option
            v-for="item in dict.type.ims_order_status"
            :key="item.value"
            :label="item.label"
            :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="下单日期" prop="time">
        <el-date-picker v-model="form.time" type="daterange" start-placeholder="开始日期" end-placeholder="结束日期"
                        value-format="yyyy-MM-dd" clearable></el-date-picker>
      </el-form-item>
      <el-form-item label="发货日期" prop="time2">
        <el-date-picker v-model="form.time2" type="daterange" start-placeholder="开始日期" end-placeholder="结束日期"
                        value-format="yyyy-MM-dd" clearable></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-button type="primary" v-hasPermi="['ims:saleMng:print']" plain icon="el-icon-printer"
               @click="okAllPrint">手动出单
    </el-button>
    <el-button type="success" v-hasPermi="['ims:saleMng:bill']" plain icon="el-icon-finished"
               @click="okAllBill">生成对账单
    </el-button>
    <el-button type="warning" v-hasPermi="['ims:saleMng:upload']" plain icon="el-icon-check"
               @click="okImg">图纸上传
    </el-button>
    <el-button type="primary" v-hasPermi="['ims:saleMng:express']" plain icon="el-icon-check"
               @click="okExpress">快递修改
    </el-button>
    <el-button type="success" v-hasPermi="['ims:saleMng:produce']" plain icon="el-icon-check"
               @click="okProduce">关联生产
    </el-button>
    <el-button type="warning" v-hasPermi="['ims:saleMng:assign']" plain icon="el-icon-check"
               @click="okAssign">分配美工
    </el-button>
    <el-button type="info" v-hasPermi="['ims:saleMng:export']" plain icon="el-icon-download"
               @click="handleExport">导出
    </el-button>
    <el-table
      v-loading="loading"
      :data="list"
      @selection-change="handleChange"
      row-key="id"
    >
      <el-table-column type="selection" width="50"></el-table-column>
      <el-table-column type="index" label="序号" align="center">
        <template slot-scope="scope">
          <span>{{ (form.pageNum - 1) * form.pageSize + scope.$index + 1 }}</span>
        </template>
      </el-table-column>
      <el-table-column prop="orderNo" label="销售单号" align="center">
        <template slot-scope="scope">
          <el-link type="primary" @click="updateRow(1,scope.row)">{{ scope.row.orderNo }}</el-link>
        </template>
      </el-table-column>
      <el-table-column prop="orderType" label="订单类型" align="center" width="80">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_order_type" :value="scope.row.orderType"/>
        </template>
      </el-table-column>
      <el-table-column prop="status" label="订单状态" align="center" width="100">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.ims_order_status" :value="scope.row.status"/>
        </template>
      </el-table-column>
      <el-table-column prop="customerName" label="客户名称" align="center" width="100"/>
      <el-table-column prop="saleName" label="销售员" align="center" width="100"/>
      <el-table-column prop="recipient" label="收件人" align="center" width="100"/>
      <el-table-column prop="content" label="订单内容" align="center" min-width="150"/>
      <el-table-column prop="expressName" label="快递公司" align="center" width="100"/>
      <el-table-column prop="expressNo" label="快递单号" align="center" width="100"/>
      <el-table-column prop="totalPrice" label="总价(元)" align="center" width="80"/>
      <el-table-column prop="urgent" label="加急单" align="center" width="90">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_yes_no" :value="scope.row.urgent"/>
        </template>
      </el-table-column>
      <el-table-column prop="orderDate" label="下单日期" align="center" width="120"/>
      <el-table-column prop="sendDate" label="发货日期" align="center" width="120"/>
      <el-table-column label="操作" align="center">
        <template slot-scope="scope">
          <el-button
            type="text"
            @click="updateRow(1,scope.row)"
          >详情
          </el-button>
          <el-button
            type="text"
            v-hasPermi="['ims:saleMng:log']"
            @click="logRow(scope.row)"
          >记录
          </el-button>
          <el-button
            v-hasPermi="['ims:saleMng:approve']"
            v-if="scope.row.status==20"
            type="text"
            @click="updateRow(2,scope.row)"
          >审核
          </el-button>
          <el-button
            v-hasPermi="['ims:saleMng:progress']"
            v-if="scope.row.orderType==10&scope.row.status>=30"
            type="text"
            @click="updateRow(3,scope.row)"
          >生产进度
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination
      :total="total"
      :page.sync="form.pageNum"
      :limit.sync="form.pageSize"
      @pagination="getList"
    />
    <detailDialog ref="detailDialog" @ok="handleQuery"></detailDialog>
    <imgUploadDialog ref="imgUploadDialog" @ok="handleQuery"></imgUploadDialog>
    <bomDialog ref="bomDialog" @ok="handleQuery"></bomDialog>
    <progressDialog ref="progressDialog" @ok="handleQuery"></progressDialog>
    <logDialog ref="logDialog" @ok="handleQuery"></logDialog>
    <expressDialog ref="expressDialog" @ok="handleQuery"></expressDialog>
    <assignDialog ref="assignDialog" @ok="handleQuery"></assignDialog>
  </div>
</template>

<script>
import {addAllBill, listSaleOrder, okAllPrint} from "../../../../api/imsMng";
import detailDialog from "../../orderMng/detailDialog.vue";
import imgUploadDialog from "./imgUploadDialog.vue";
import bomDialog from "./bomDialog.vue";
import progressDialog from "./progressDialog.vue";
import logDialog from "./logDialog.vue";
import expressDialog from "./expressDialog.vue";
import {formatDay} from "../../../../utils";
import assignDialog from "./assignDialog.vue";

export default {
  name: 'ProduceMng',
  components: {detailDialog, imgUploadDialog, bomDialog, progressDialog, logDialog, expressDialog, assignDialog},
  dicts: ['ims_order_status', 'sys_yes_no', 'sys_order_type'],
  data() {
    return {
      tabIndex: '10', //10定制，20现货
      loading: false,
      list: [],
      form: {
        orderType: "",
        keyWord: "",
        orderNo: "",
        statusList: "",
        customerName: "",
        time: [],
        time2: [],
        pageNum: 1,
        pageSize: 10
      },
      total: 0,
      selectList: [],
      selectIdList: [],
    }
  },
  created() {
    this.load()
  },
  methods: {
    load() {
      this.handleQuery()
    },
    handleQuery() {
      this.form.pageNum = 1
      this.getList()
    },
    resetQuery() {
      this.resetForm('form')
      this.handleQuery()
    },
    handleChange(e) {
      this.selectList = e
      e.map(val => {
        this.selectIdList.push(val.id)
      })
    },
    okAllPrint() {
      if (!this.selectList.length) {
        this.$message.error('请先选择至少一条数据')
        return
      }
      this.$modal.confirm('是否确定手动出单').then(() => {
        okAllPrint({
          ids: this.selectIdList
        }).then(res => {
          this.handleQuery()
          this.$message.success('出单成功')
        })
      })
    },
    okAllBill() {
      if (!this.selectList.length) {
        this.$message.error('请先选择至少一条数据')
        return
      }
      this.$modal.confirm('是否确定生成对账单').then(() => {
        addAllBill({
          ids: this.selectIdList
        }).then(res => {
          this.handleQuery()
          this.$message.success('生成成功')
        })
      })
    },
    okImg() {
      if (this.selectList.length != 1) {
        this.$message.error('请先选择一条数据')
        return
      }
      let req = {
        id: this.selectList[0].id,
        orderNo: this.selectList[0].orderNo
      }
      this.$refs.imgUploadDialog.show(req)
    },
    okExpress() {
      if (this.selectList.length != 1) {
        this.$message.error('请先选择一条数据')
        return
      }
      let req = {
        id: this.selectList[0].id,
        orderNo: this.selectList[0].orderNo,
        expressId: this.selectList[0].expressId,
        recipientContent: this.selectList[0].recipientContent
      }
      this.$refs.expressDialog.show(req)
    },
    okProduce() {
      if (this.selectList.length != 1) {
        this.$message.error('请先选择一条数据')
        return
      }
      let req = {
        id: this.selectList[0].id,
        orderNo: this.selectList[0].orderNo
      }
      this.$refs.bomDialog.show(req)
    },
    okAssign() {
      if (this.selectList.length != 1) {
        this.$message.error('请先选择一条数据')
        return
      }
      let req = {
        id: this.selectList[0].id,
        orderNo: this.selectList[0].orderNo
      }
      this.$refs.assignDialog.show(req)
    },
    getList() {
      this.loading = true
      if (this.form.time && this.form.time.length) {
        this.form.startTime = this.form.time[0]
        this.form.endTime = this.form.time[1]
      } else {
        this.form.startTime = ''
        this.form.endTime = ''
      }
      if (this.form.time2 && this.form.time2.length) {
        this.form.sendStartTime = this.form.time2[0]
        this.form.sendEndTime = this.form.time2[1]
      } else {
        this.form.sendStartTime = ''
        this.form.sendEndTime = ''
      }
      listSaleOrder(this.form).then(res => {
        this.list = res.rows
        this.total = res.total
      }).finally(e => {
        this.loading = false
      })
    },
    handleExport() {
      this.download('/ims/customer/order/export', {
        ...this.form
      }, `销售订单_${formatDay(new Date())}.xlsx`)
    },
    logRow(row) {
      this.$refs.logDialog.show(row)
    },
    updateRow(flag, row) {
      let form = JSON.parse(JSON.stringify(row))
      if (flag == 1) {
        row.isSaler = 1
        this.$refs.detailDialog.show(row)
      }
      if (flag == 2) {
        this.$refs.detailDialog.show(row, 1)
      }
      if (flag == 3) {
        this.$refs.progressDialog.show(form)
      }
    }
  }
}
</script>
